package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.InstallmentFeeTableReq;
import com.jrx.anytxn.param.dto.product.InstallmentFeeTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IInstallmentFeeTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 分期手续费表
 *
 * @author Yangxin
 * @date 2019/8/2
 */
@Api(tags = "页面分期手续费参数API接口")
@RestController
@RequestMapping(value = "/param/installments")
public class InstallmentFeeTableController {
    private static final Logger logger = LoggerFactory.getLogger(InstallmentFeeTableController.class);
    @Autowired
    IInstallmentFeeTableService iInstallmentFeeTableService;

    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和费用ID查询分期手续费表信息", notes = "根据租户ID和费用ID查询分期手续费表信息")
    @GetMapping(value = "/{tenantId}/{installmentFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "installmentFeeId", value = "分期手续费ID", required = true)
    })
    public TxnRespResult<InstallmentFeeTableRes> getById(@NotNull @PathVariable String tenantId, @PathVariable String installmentFeeId) {
        TxnRespResult<InstallmentFeeTableRes> result = new TxnRespResult<>();
        InstallmentFeeTableRes res;
        try {
            res = iInstallmentFeeTableService.findByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), installmentFeeId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询分期手续费表信息", notes = "分页查询分期手续费表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<InstallmentFeeTableRes>> getAllPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<InstallmentFeeTableRes>> result = new TxnRespResult<>();
        TxnPage<InstallmentFeeTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iInstallmentFeeTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增分期手续费表信息", notes = "新增分期手续费表信息")
    @PostMapping("/")
    public TxnRespResult<InstallmentFeeTableRes> createInstallmentFeeTable(@RequestBody InstallmentFeeTableReq installmentFeeTableReq) {
        InstallmentFeeTableRes res;
        TxnRespResult<InstallmentFeeTableRes> result = new TxnRespResult<>();
        try {
            installmentFeeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            installmentFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iInstallmentFeeTableService.addInstallmentFeeTable(installmentFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑分期手续费表信息", notes = "根据租户ID编辑分期手续费表信息")
    @PutMapping("/")
    public TxnRespResult<InstallmentFeeTableRes> modifyInstallmentFeeTable(@RequestBody InstallmentFeeTableReq installmentFeeTableReq) {
        TxnRespResult<InstallmentFeeTableRes> result = new TxnRespResult<>();
        InstallmentFeeTableRes res;
        try {
            installmentFeeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iInstallmentFeeTableService.modifyInstallmentFeeTable(installmentFeeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和费用ID删除分期手续费表信息", notes = "根据租户ID和费用ID删除分期手续费表信息")
    @DeleteMapping(value = "/{tenantId}/{installmentFeeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "installmentFeeId", value = "分期手续费ID", required = true)
    })
    public TxnRespResult<Boolean> removeById(@NotNull @PathVariable String tenantId, @PathVariable String installmentFeeId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iInstallmentFeeTableService.removeByTenIdAndFeeId(String.valueOf(infoService.getUser().getProjectId()), installmentFeeId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    //或删  ??
    @ApiOperation(value = "通过费用ID及机构ID获取费用信息", notes = "通过费用ID及机构ID获取费用信息")
    @GetMapping(value = "/InstallmentFeeId/{installmentFeeId}/orgId/{orgId}/")
    public TxnRespResult<InstallmentFeeTableRes> getByInstallmentFeeIdAndOrgId(@NotNull @PathVariable String installmentFeeId, @NotNull @PathVariable String orgId) {
        TxnRespResult<InstallmentFeeTableRes> result = new TxnRespResult<>();
        InstallmentFeeTableRes installmentFeeTableRes;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            installmentFeeTableRes = iInstallmentFeeTableService.findByInstallmentFeeId(installmentFeeId, orgId,tenantId);
            result.getSuccess(installmentFeeTableRes);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.getFail(TxnRespCode.ERROR.getCode(), e.getMessage());
        }
        return result;
    }

}
